package com.lagou.user.controller;

import com.lagou.email.service.CodeEmailService;
import com.lagou.user.entity.UserInfo;
import com.lagou.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    public UserService userService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Reference
    private CodeEmailService codeEmailService;


    @GetMapping("/register/{email}/{password}/{code}")
    public Integer registerUser(@PathVariable String email, @PathVariable String password,@PathVariable String code) {

        System.out.println("====>>>>>>>>>>>>>>UserController registerUser，访问到我这里了......"+code+email);
        int codeCur = codeEmailService.validateEmailCode(email,code);
        if( codeCur != 0){
            return 2;
        }

        UserInfo user = userService.getUserInfoByEmail(email);
        if(user != null){
            return 3;
        }

        int isRegiste = userService.register(email, password);

        return 0;
    }

    // 是否已注册，根据邮箱判断,true代表已经注册过，
    //false代表尚未注册
    @GetMapping("/isRegistered/{email}")
    public Integer isRegistered(@PathVariable String email) {

        System.out.println("====>>>>>>>>>>>>>>isRegistered，访问到我这里了......"+email);
        UserInfo user = userService.getUserInfoByEmail(email);
        if(user != null){
            return 1;
        }else {
            return 0;
        }
    }

    // 登录接⼝口，验证⽤用户名密码合法性，根据⽤用户名和
    //密码⽣生成token， token存⼊入数据库，并写⼊入cookie
    //中，登录成功返回邮箱地址，重定向到欢迎⻚页
    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletResponse response ) {

        System.out.println("====>>>>>>>>>>>>>>login，访问到我这里了......"+ email);

        UserInfo user = userService.getUserInfoByEmail(email);
        if(user == null){
            return "0";//"账号不存在";
        }
        if(!password.equals(user.getPassword())){
            return "1";//"密码不正确";
        }

        String token = UUID.randomUUID().toString();
        Cookie cookie =new Cookie("token",token);
        response.addCookie(cookie);
        redisTemplate.opsForValue().set("token:"+token,email);
//        redisTemplate.opsForValue().set("token:"+token,email,30, TimeUnit.MINUTES);

        return token;
    }

    @GetMapping("/info/{token}")
    public String tonkenInfo(@PathVariable String token  ) {

        System.out.println("====>>>>>>>>>>>>>>我是code，访问到我这里了......"+ token);

        String s = redisTemplate.opsForValue().get("token:" + token);

        System.out.println("====>>>>>> tonkenInfo "+ s);

        if(StringUtils.isEmpty(s)){
            return "token不存在";
        }

        return s;
    }


}
